package com.dongge.poi.hold;

import org.apache.poi.ss.usermodel.*;


public class StyleHold {
    private CellStyle dateStyle;
    private CellStyle defaultStyle;
    private CellStyle dataStyle;
    private CreationHelper createHelper;
    private DataFormat dataFormat;


    public StyleHold(Workbook wb) {
        createHelper = wb.getCreationHelper();
        dataFormat = wb.createDataFormat();
        Font font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 24);
        //设置字体名字
        font.setFontName("宋体");

        defaultStyle = wb.createCellStyle();
        baseStyle(defaultStyle);
        defaultStyle.setFont(font);


        dateStyle = wb.createCellStyle();
        dateStyle.setFont(font);

        baseStyle(dateStyle);

        dateStyle.setDataFormat(
                createHelper.createDataFormat().getFormat("m/d/yy h:mm"));

        dataStyle = wb.createCellStyle();
        dataStyle.setDataFormat(dataFormat.getFormat("#,##0.00"));
        baseStyle(dataStyle);
    }

    private void baseStyle(CellStyle style) {
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);

        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //自动换行
        style.setWrapText(true);
    }

    public CellStyle getDateStyle() {
        return dateStyle;
    }

    public CellStyle getDefaultStyle() {
        return defaultStyle;
    }

    public CellStyle getDataStyle() {
        return dataStyle;
    }
}
